/****************************************************
	文件：CompoundTask.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/8/26 17:48:36
	功能：复合任务（框架层）
*****************************************************/

using System;
using System.Collections.Generic;

namespace AIFramework.HTN
{
    public class Method
    {
        public int Id;
        /// <summary>
        /// 前置条件
        /// </summary>
        public Func<WorldState, bool> CheckCond = (ws) => true;
        /// <summary>
        /// 方法下的子任务
        /// </summary>
        public List<TaskBase> SubTasks;

        public Method(int id)
        {
            Id = id;
            SubTasks = new List<TaskBase>();
        }

        /// <summary>
        /// 添加前置条件
        /// </summary>
        /// <param name="cond">前置条件委托</param>
        /// <returns></returns>
        public Method AddCond(Func<WorldState, bool> cond)
        {
            CheckCond = cond;
            return this;
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="task">子任务</param>
        /// <returns></returns>
        public Method AddTask(TaskBase task)
        {
            if (task != null)
            {
                SubTasks.Add(task);
            }
            return this;
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="task">子任务</param>
        /// <returns></returns>
        public Method AddTasks(TaskBase task1, TaskBase task2)
        {
            if (task1 != null && task2 != null)
            {
                SubTasks.Add(task1);
                SubTasks.Add(task2);
            }
            return this;
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="task">子任务</param>
        /// <returns></returns>
        public Method AddTasks(TaskBase task1, TaskBase task2, TaskBase task3)
        {
            if (task1 != null && task2 != null && task3 != null)
            {
                SubTasks.Add(task1);
                SubTasks.Add(task2);
                SubTasks.Add(task3);
            }
            return this;
        }
    }

    public class CompoundTask : TaskBase
    {
        /// <summary>
        /// 前置条件
        /// </summary>
        public Func<WorldState, bool> CheckCond;
        /// <summary>
        /// 可选方法集合
        /// </summary>
        protected List<Method> mMethods = new List<Method>();

        public CompoundTask(string name, TaskType taskType) : base(name, taskType)
        {
            mMethods = new List<Method>();
            CheckCond = (ws) => true;
        }

        public void Reset()
        {
            mMethods.Clear();
            CheckCond = (ws) => true;
        }

        /// <summary>
        /// 添加前置条件s
        /// </summary>
        /// <param name="cond">前置条件委托</param>
        /// <returns></returns>
        public CompoundTask AddCond(Func<WorldState, bool> cond)
        {
            CheckCond = cond;
            return this;
        }
        /// <summary>
        /// 添加复合任务中的可选方法
        /// </summary>
        /// <param name="method">方法对象</param>
        public CompoundTask AddMethod(Method method)
        {
            mMethods.Add(method);
            return this;
        }
        /// <summary>
        /// 寻找首个满足条件的可选方法
        /// </summary>
        /// <param name="ws">世界状态</param>
        /// <returns></returns>
        public Method FindValidMethod(WorldState ws)
        {
            foreach (var method in mMethods)
            {
                if (method.CheckCond(ws))
                {
                    return method;
                }
            }
            return null;
        }
    }
}
